/*
RippleVoiceFactory.buildVoice("",  \rippleSine);
*/


RippleVoiceFactory{

	*buildVoice{|path, synthType, name|
	
		var voice, player, controlsFactory, controls, gui;
		
		voice = RippleVoice.new.init;
		voice.setSynthType(synthType);
		
		if(path.notNil){
			voice.setAudioFilePath(path);
		};
			
		if(name == nil && path.notNil){
			name =  this.getNameFromPath(path);
		};
		
		name = synthType.createSynthName(name);
	
		name = this.enforceUniqueName(name);
	
		voice.setName(name);
		

		player = synthType.getPlayerFactoryClass.createSlFromPath(path);

		player.setParent(voice);
		voice.setPlayer(player);
		
		controlsFactory = ControlsFactory.new;
		controlsFactory.setSynthType(synthType);

		controls = controlsFactory.makeControls.setParent(voice);
		voice.setControls(controls);
		
		synthType.getBehaviorClasses.do{|behaviorClass|
			voice.addBehavior(behaviorClass.new(voice));
		};	
		
		gui = VoiceGui.new;
		gui.setParent(voice);
		voice.setGui(gui);
		
		voice.setPulser(RippleVoicePulser.new(voice));
		
		^voice;
		
	}
	
	*enforceUniqueName{|baseName|
		var finalName, i = 1;
		
		("baseName: " + baseName).postln;
	
		
		finalName = baseName;
		
		// add a number to the name if it already exists
		while(
			{
				Ripple.voiceExists(finalName);
			},
			{
				i = i + 1;	
				finalName = baseName + (i.asString);
			}
		);
		^finalName;
	}
	
	*getNameFromPath{|path|
		var arr, baseName, finalName;
		arr = path.split;
		baseName = arr.at(arr.size -1);
		^baseName;
	}

}